package com.example.watermark;

import com.example.model.WaterSensor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WaterMarkTask
 * Package: com.example.watermark
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-25
 * Time: 13:28
 */

//多并行度问题
public class WaterMarkTask {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        SingleOutputStreamOperator<WaterSensor> map = env.socketTextStream("hadoop102", 7777)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        final String[] s = value.split(" ");

                        return new WaterSensor(s[0], Long.valueOf(s[1]), Integer.valueOf(s[2]));
                    }
                });

        //设置水位线分配策略
        WatermarkStrategy<WaterSensor> strategy = WatermarkStrategy
                //单调升序
                .<WaterSensor>forMonotonousTimestamps()
                //在多并行度的情况下 上游传到下游的事件时间是取最小的值为准
                //但是存在其中一个上游迟迟不更新时间线 那么就会一直以最小的为准 等下去
                //withIdleness就是这只一个等待时间 超过3秒不更新数据 就以我的最小为准了
                .withIdleness(Duration.ofSeconds(3))
                //从数据中提取时间戳
                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                    @Override
                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                        return element.getTs() * 1000L;
                    }
                });

        SingleOutputStreamOperator<WaterSensor> watermarks =
                 map.assignTimestampsAndWatermarks(strategy);

        //keyBy
        SingleOutputStreamOperator<String> process = watermarks.keyBy(value -> value.getId())
                //开窗 1设置开窗策略 2.窗口函数
                //滑动事件时间窗口
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                //设置窗口延迟关闭 达到了窗口时间 触发计算 但是延迟3秒之后 才会关闭 后来的数据追到后面
                .allowedLateness(Time.seconds(3))
                //已经延迟关闭窗口的数据 还迟到了 那么从测输出流中输出
                .sideOutputLateData(new OutputTag<>("tag", Types.POJO(WaterSensor.class)))
                //窗口函数 输入类型 输出类型 key的类型 窗口类型
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    //key的类型 上下文 传出进来的数据集合 采集器
                    @Override
                    public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        final long start = context.window().getStart();
                        final long end = context.window().getEnd();
                        final String st = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss");
                        String en = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss");

                        //获取条数
                        long l = elements.spliterator().estimateSize();

                        out.collect("key=" + s + " 的窗口的时间[ " + st + "," + en + "] 包含 " + l + "  条数据: " + elements);

                    }
                });

        process.print();
        env.execute();
    }

}
